<?php
namespace app\shops\controller;

use think\Session;
use think\Db;
use think\Model;
use app\plug\controller\qiniu\Qiniu;

/**
 * 投票管理
 */
class VotePlayer extends Base
{
    /**
     * @throws
     * 投票列表
     * @return [type] [description]
     */
    public function index()
    {
        // 获取配置信息
        $conf = config();
        // 分页信息
        $pageSize   = input('limit')    ? input('limit')    : $conf['paginate']['list_rows'];

        // 获取前台传过来的姓名查询条件
        $name = input('get.keywords') ? : "";
        $this->assign('name',$name);
        $pageParam['query']['name'] = $name;

        // 获取前台传过来的活动查询条件
        $id = input('get.keywords1') ? : "";
        $this->assign('keywords1',$id);
        $pageParam['query']['id'] = $id;


        // 列表显示所有投票选手列表
        $list = Db("vote_player")->alias('vp')->join('vote_act va','vp.act_id=va.id')
            ->field('va.title, vp.id, vp.act_id, vp.name, vp.pic_url, vp.vote_num, vp.tel, vp.status, vp.create_time, vp.check')
            ->where('vp.status',0)
            ->where('vp.name','like',"%{$name}%")
            ->where('va.id','like',"%{$id}%")
            ->order("va.id desc")->paginate(['query'=>request()->param(),$pageSize]);

        $list_to_arr = $list->toArray();
        $new_list = [];
        if ($list_to_arr['total']) {
            foreach ($list_to_arr['data'] as $k => $v) {
                $new_list[$k] = $v;
                $new_list[$k]['pic_url'] = json_decode($v['pic_url']);
            }
        }

        //活动信息
        $vote_act = Db('vote_act')->field('id,title')->select();

        $this->assign([
            'total'     => $list->total(),
            'page'      => $list->render(),
            'list'      => $new_list,
            'vote_act'  => $vote_act
        ]);

        return $this->fetch();

    }


    /**
     * 投票记录
     */
    public function vote_recode(){
        // 获取配置信息
        $conf = config();
        // 分页信息
        $pageSize   = input('limit')    ? input('limit')    : $conf['paginate']['list_rows'];

        // 列表显示该选手的所有投票记录
        $list = Db("vote_recode")->where('player_id',input('id'))->paginate($pageSize);

        $this->assign([
            'total'         => $list->total(),
            'page'          => $list->render(),
            'list'          => $list
        ]);

        return $this->fetch();
    }


    /**
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     * 审核
     */
    public function status(){
        if (request()->isPost()) {
            $id = input('id');
            $res = Db::name('vote_player')->where('id', $id)->update(['check' => input('check')]);
            if ($res) {
                $this->success('操作成功');
            } else {
                $this->error('操作失败');
            }
        }
    }

    /**
     * 软删除
     */
    public function del()
    {
        if (request()->isPost()) {
            $id = input('id');
            $res = Db('vote_player')->where('id', $id)->update(['status' => 1]);
            if ($res) {
                $this->success("删除成功");
            } else {
                $this->error("删除失败");
            }
        }
    }

}
